<?php

namespace app\admin\middleware;

use app\admin\service\AdminService;
use app\admin\service\RbacService;
use app\Request;
use think\facade\View;

class AdminAuth
{
    public function handle($request, \Closure $next)
    {
        $noNeedLogin = $request->noNeedLogin ?? [];
        if(self::_checkActionMatch($request->action(),$noNeedLogin)){
            // 不需要验证登录
            return $next($request);
        }else{
            if(!AdminService::instance()->isLogin()){
                //未登录处理
                return self::_handleUnLogin($request);
            }

            $noNeedPermission = $request->noNeedPermission ?? [];
            if(self::_checkActionMatch($request->action(),$noNeedPermission)){
                // 不需要验证权限
                return $next($request);
            }else{
                $adminInfo = AdminService::instance()->getAdminInfo();
                if(!$adminInfo['status']){
                    //账号被禁用处理
                    return self::_handleUnNomal($request);
                }
                $app = app('http')->getName();
                $controller = $request->controller();
                $action = $request->action();
                if(!RbacService::auth($adminInfo['role_id'],$app,$controller,$action)['status']){
                    //未验证权限通过
                    return self::_handleUnAuth($request);
                }
            }
        }
        return $next($request);
    }

    /**
     * 验证方法是否匹配
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @param [type] $action
     * @param [type] $arr
     * @return bool
     */
    public static function _checkActionMatch(string $action,array $arr){
        if(empty($arr)){
            return false;
        }
        $arr = array_map('strtolower',$arr);
        if(in_array(strtolower($action),$arr) || in_array('*',$arr)){
            return true;
        }
        return false;
    }

    /**
     * 未登录处理
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @param Request $request
     * @return void
     */
    public static function _handleUnLogin(Request $request){
        if($request->isAjax()){
            return json(['status'=>false,'code' => 400,'msg' => '未登录,请刷新登陆!']);
        }else{
            return redirect('/admin/Login/login');
        }
    }

    /**
     * 未验证权限通过处理
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @param Request $request
     * @return void
     */
    public static function _handleUnAuth(Request $request){
        if($request->isAjax()){
            return json(['status'=>false,'code' => 403,'msg' => '没有权限访问!']);
        }else{
            exit('没有权限访问!');
        }
    }

    /**
     * 账号已被禁用处理
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @param Request $request
     * @return void
     */
    public static function _handleUnNomal(Request $request){
        if($request->isAjax()){
            return json(['status'=>false,'code' => 403,'msg' => '账号已被禁用!']);
        }else{
            exit('账号已被禁用!');
        }
    }
}